package com.hpe.wx.performance.mongo.jmxObj.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hpe.wx.performance.mongo.jmxObj.dao.JmxObjDao;
import com.hpe.wx.performance.mongo.jmxObj.domain.JmxObj;

@Service
@Transactional
public class JmxObjServiceImpl implements JmxObjService {
	@Autowired
	private JmxObjDao dao;

	private static final Logger logger = Logger.getLogger(JmxObjServiceImpl.class);

	@Override
	public void addJmxUrl(JmxObj jmxUrl) {
		dao.save(jmxUrl); 
	}

	@Override
	public void addJmxUrl(String jmxName, String jmxUrlStr) {
		JmxObj jmxObj = new JmxObj();
		jmxObj.setUrl(jmxUrlStr);
		jmxObj.setName(jmxName);
		Date createDateTime = new Date();
		jmxObj.setCreateTime(createDateTime);
		jmxObj.setCreatedBy(124l);
		jmxObj.setStatus(1);
		dao.save(jmxObj);
	}

	@Override
	public void delByUrl(String jmxUrl) {
		JmxObj res = null;
		List<JmxObj> temp = dao.findByUrl(jmxUrl) ;
		if(temp != null && temp.size() > 0){
			res = temp.get(0);
			res.setStatus(0);
		}
		dao.save(res);
		//dao.delete(res);
	}

	@Override
	public void delByName(String name) {
		JmxObj res = null;
		List<JmxObj> temp =  dao.findByName(name) ;
		if(temp != null && temp.size() > 0){
			res = temp.get(0);
			res.setStatus(0);
		} 
		dao.save(res);
	}

	@Override
	public void updateJmxUrl(JmxObj jmxUrl) {
		dao.save(jmxUrl);
	}

	@Override
	public JmxObj getByUrl(String jmxUrl) {
		JmxObj res = null;
		List<JmxObj> temp =  dao.findByUrl(jmxUrl);
		if(temp != null && temp.size() > 0){
			res = temp.get(0); 
		}
		return res;
	}

	@Override
	public JmxObj getByName(String name) {
		JmxObj res = null;
		List<JmxObj> temp = dao.findByName(name);
		if(temp != null && temp.size() > 0){
			res = temp.get(0); 
		} 
		return res;
	}

	@Override
	public List<JmxObj> getAllJmxUrl() {
		List<JmxObj> res =null;
		res = dao.findAll();
		return res;
	}

	@Override
	public List<JmxObj> getByStatus(Integer status) {
		List<JmxObj> res =null;
		res = dao.findByStatus(status);
		return res;
	}

	@Override
	public JmxObj getById(String id) {
		JmxObj res = dao.findOne(id);
		return res;
	}
}
